package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {


    //依赖注入RoleService
    @Reference
    private RoleService roleService;


    /*
    * 删除角色
    * */
    @RequestMapping("delById")
    public Result delById(Integer id){

        try {
            roleService.delById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /*
    * 编辑角色
    * */
    @RequestMapping("edit")
    public Result edit(@RequestBody Role formData, Integer[] permissionIds, Integer[] menuIds){
        try{

            roleService.edit(formData,permissionIds,menuIds);
            return new Result(true, MessageConst.ACTION_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /*
    * 编辑角色-- 点击编辑按钮,基于角色id回显数据到表格
    * */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try{
            Role role = roleService.findById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS,role);

        }catch (Exception e){

            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }

    }

    /*
    * 编辑角色-- 回显权限项中角色id关联的权限集合
    * 思路: 在
    * */
    @RequestMapping("findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){

        try{
            List<Integer> permissionList=roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConst.ACTION_SUCCESS,permissionList);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);

        }
    }

    /*
     * 编辑角色-- 回显权限项中角色id关联的权限集合
     * 思路: 在
     * */
    @RequestMapping("findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){

        try{
            List<Integer> menuList=roleService.findMenuIdsByRoleId(id);
            return new Result(true, MessageConst.ACTION_SUCCESS,menuList);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);

        }
    }

    /*
     * 用户管理新建和编辑角色时, 回显角色数据
     *
     * */
    @RequestMapping("/findAll")
    public Result findAll(){

        try{
            List<Role> roleList = roleService.findAll();
            return new Result(true,MessageConst.ACTION_SUCCESS,roleList);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }


    /*新增角色*/
    @RequestMapping("add")
    public Result add(@RequestBody Role formDate, Integer[] permissionIds, Integer[] menuIds){

        try{

            roleService.add(formDate,permissionIds,menuIds);
            return new Result(true, MessageConst.ACTION_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }

    }
    /*
    * 分页查询
    * */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        try{
            PageResult pageResult = roleService.pageQuery(queryPageBean);
            return pageResult;


        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(0L,null);

        }

    }

}
